<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search_model extends CI_Model
{	
	public function get_any_users()
	{
		$this->db->select("*");
		$this->db->from("userbaseinfo");
		$this->db->where("uid !=",get_uid());
		$this->db->limit(100);
		$query = $this->db->get();
		$rows = $query->result();
		
		return $rows;
	}
	
	public function get_users_by_name($keyword)
	{
		return array("user1", "user2", "user3");
	}
	
	private function get_uid_array($result)
	{
		if(!$result)
			return array();
		
		$ret = array();
		foreach ($result as $record)
		{
			$ret[] = $record->uid;
		}
		
		return $ret;
	}
	
	/*
	 * return array of uid
	 */
	private function search_sympotm($keyword)
	{
		$relevant_symptom = array();
		
		$map = get_symptom_map();
		
		for($i=0; $i < count($map); $i ++)
		{
			$symptom_name = $map[$i];
			if(stristr($symptom_name, $keyword))
				$relevant_symptom[] = $i;
		}
		
		if(!$relevant_symptom)
			return;
		
		$this->db->where_in("symptomType", $relevant_symptom);
		$query = $this->db->get("usersymptom");
		
		$result = $query->result();
		
		return $this->get_uid_array($result);
	}
	
	/*
	 * return array of uid
	*/
	private function search_nick_name($keyword)
	{
		$this->db->like("nickname", $keyword);
		$query = $this->db->get("userlogin");
		$result = $query->result();
		return $this->get_uid_array($result);
	}
	
	/*
	 * return array of uid
	*/
	private function search_user_name($keyword)
	{
		$sql = "select * from userbaseinfo where concat(lastName, firstName) like binary '%$keyword%';";
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return $this->get_uid_array($result);
	}
	
	public function get_relevant_user($keyword)
	{
		$by_user_name = $this->search_user_name($keyword);
		$by_nick_name = $this->search_nick_name($keyword);
		$by_symptom = $this->search_sympotm($keyword);
		
		return array(
				"by_user_name"=>$by_user_name,
				"by_nick_name"=>$by_nick_name,
				"by_symptom"=>$by_symptom
				);
	}
}
?>